package com.wytec.xywzxw.configuration;

import com.wytec.xywzxw.bean.User;
import com.wytec.xywzxw.constant.MyConstant;
import com.wytec.xywzxw.service.AccessLogService;
import com.wytec.xywzxw.service.UserService;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class MyVerificationInterceptor implements HandlerInterceptor {
    @Autowired
    AccessLogService logService;

    @Autowired
    UserService userService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            // 用于数据有效性校验
            String token = request.getHeader(MyConstant.TOKEN);

            if (handlerMethod.hasMethodAnnotation(MyTokenVerification.class)) {
                MyTokenVerification tokenVerification = handlerMethod.getMethodAnnotation(MyTokenVerification.class);
                //根据注解条件认证
//                User userInfo = userService.getUserInfo(token);

            }
            if (handlerMethod.hasMethodAnnotation(ApiOperation.class)) {
                ApiOperation apiOperation = handlerMethod.getMethodAnnotation(ApiOperation.class);

                logService.insertAccessLog(request, apiOperation.value(), "");
            }


            return true;

        }
        return true;
    }
}
